/**
 * Create by CaoWenjian
 *
 * @copyRight: 2018
 **/
package com.bjboot.caocao.system.controller;

import com.bjboot.caocao.common.config.Constant;
import com.bjboot.caocao.common.utils.Result;
import com.bjboot.caocao.system.annotation.BJLog;
import com.bjboot.caocao.system.domain.MenuDO;
import com.bjboot.caocao.system.domain.Tree;
import com.bjboot.caocao.system.service.MenuService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *@Description 菜单管理控制器
 *@Author 操文健
 *@Date 2018-09-18 上午11:35
 *@Version 1.0
 **/
@RequestMapping("/sys/menu")
@Controller
public class MenuController extends UserBaseController {

    private String prefix = "system/menu";
    @Autowired
    MenuService menuService;

    /**
     * 系统根目录
     * @param model
     * @return
     */
    @ApiOperation(value="获取菜单根目录module")
    @RequiresPermissions("sys:menu:menu")
    @GetMapping()
    String menu(Model model) {
        return prefix+"/menu";
    }

    @ApiOperation(value="根据参数查询菜单")
    @RequiresPermissions("sys:menu:menu")
    @RequestMapping("/list")
    @ResponseBody
    List<MenuDO> list(@RequestParam Map<String, Object> params) {
        List<MenuDO> menus = menuService.list(params);
        return menus;
    }

    @ApiOperation(value="添加菜单Module")
    @BJLog("添加菜单")
    @RequiresPermissions("sys:menu:add")
    @GetMapping("/add/{pId}")
    String add(Model model, @PathVariable("pId") Long pId) {
        model.addAttribute("pId", pId);
        if (pId == 0) {
            model.addAttribute("pName", Constant.TREE_ROOT_NAME);
        } else {
            model.addAttribute("pName", menuService.get(pId).getName());
        }
        return prefix + "/add";
    }

    @ApiOperation(value="修改菜单Module")
    @BJLog("编辑菜单")
    @RequiresPermissions("sys:menu:edit")
    @GetMapping("/edit/{id}")
    String edit(Model model, @PathVariable("id") Long id) {
        MenuDO mdo = menuService.get(id);
        Long pId = mdo.getParentId();
        model.addAttribute("pId", pId);
        if (pId == 0) {
            model.addAttribute("pName", Constant.TREE_ROOT_NAME);
        } else {
            model.addAttribute("pName", menuService.get(pId).getName());
        }
        model.addAttribute("menu", mdo);
        return prefix+"/edit";
    }

    @ApiOperation(value="保存菜单")
    @BJLog("保存菜单")
    @RequiresPermissions("sys:menu:add")
    @PostMapping("/save")
    @ResponseBody
    Result save(MenuDO menu) {
        if (menuService.save(menu) > 0) {
            return Result.ok();
        } else {
            return Result.error(1, "保存失败");
        }
    }

    @ApiOperation(value="更新菜单")
    @BJLog("更新菜单")
    @RequiresPermissions("sys:menu:edit")
    @PostMapping("/update")
    @ResponseBody
    Result update(MenuDO menu) {
        if (menuService.update(menu) > 0) {
            return Result.ok();
        } else {
            return Result.error(1, "更新失败");
        }
    }

    @ApiOperation(value="删除菜单")
    @BJLog("删除菜单")
    @RequiresPermissions("sys:menu:remove")
    @PostMapping("/remove")
    @ResponseBody
    Result remove(Long id) {
        if (menuService.remove(id) > 0) {
            return Result.ok();
        } else {
            return Result.error(1, "删除失败");
        }
    }

    @ApiOperation(value="获取菜单树")
    @GetMapping("/tree")
    @ResponseBody
    Tree<MenuDO> tree() {
        Tree<MenuDO>  tree = menuService.getTree();
        return tree;
    }

    @ApiOperation(value="根据角色Id获取菜单树")
    @GetMapping("/tree/{roleId}")
    @ResponseBody
    Tree<MenuDO> tree(@PathVariable("roleId") Long roleId) {
        Tree<MenuDO> tree = menuService.getTree(roleId);
        return tree;
    }
}
